﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CashNote
{
    public partial class NoteForm : Form
    {
        private bool b = false;
        private double dId = 0 ;
        private string sNote = "";
        private string sBt = "";
        private string sBz = "";
        private connTest conntest = new connTest();
        public NoteForm()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            b = true;
        }

        private void NoteForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isModified())
            {
                DialogResult dr = MessageBox.Show("是否保存？", "关闭", MessageBoxButtons.YesNoCancel);

                if ( dr == DialogResult.Yes)
                {
                    int i = -1;
                    if (dId > 0)
                    {
                        i = updateNoteData();
                    }
                    else
                    {
                        i = insertNoteData();
                    }

                    if ( i < 0)
                    {
                        MessageBox.Show("保存失败");
                        e.Cancel = true;
                    }
                    else
                    {
                        e.Cancel = false;
                    }
                }
                else if (dr == DialogResult.No)
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private int insertNoteData()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("insert into t_mem (input_date,neirong,staff_id,biaoti,beizhu,modify_date,staff_name )");
            sb.Append(" values ");
            sb.Append(" ('"+System.DateTime.Now +"', '"+this.textBox1.Text.Trim()+"','"+ Global.StrUser +"',");
            sb.Append(" '" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "', '" + System.DateTime.Now + "', '" + Global.StrUserName + "')");
            if (conntest.Insert(sb.ToString(), Global.Conn))
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        private int updateNoteData()
        {
            string sql = "update [t_mem] set [neirong] = '" + this.textBox1.Text.Trim() + "' , "+
                " [biaoti] = '" + textBox2.Text.Trim() + "', " +
                " [beizhu] = '" + textBox3.Text.Trim() + "', " +
                "[modify_date] = #" + System.DateTime.Now + "# " + 
                " where [id] = "+ dId.ToString() +" ";

            if (conntest.Update(sql,Global.Conn))
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        public void setDate(double id,string nr,string bt,string bz)
        {
            this.textBox1.Text = nr;
            textBox2.Text = bt;
            textBox3.Text = bz;
            dId = id;
        }

        private void NoteForm_Load(object sender, EventArgs e)
        {
            sNote = this.textBox1.Text.Trim();
            sBt = textBox2.Text.Trim();
            sBz = textBox3.Text.Trim();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            b = true;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            b = true;
        }

        private bool isModified()
        {
            if (b.Equals(true) && (!sBt.Equals(textBox2.Text.Trim()) || !sNote.Equals(textBox1.Text.Trim()) || !sBz.Equals(textBox3.Text.Trim())))
            {
                return true;
            }else
            {
                return false;
            }
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //Alt + F4
                SendKeys.Send("%{F4}");
            }
        }
    }
}
